class Solution {
    public int majorityElement(int[] nums) {
        if(nums == null || nums.length == 0) {
            return 0;
        }
        if(nums.length == 1) {
            return nums[0];
        }
        HashMap<Integer,Integer> hashMap = new HashMap<>();
        int length = nums.length;
        int num = 0;
        for(int i = 0;i < length;i++) {
            if(hashMap.containsKey(nums[i])) {
                int a = hashMap.get(nums[i]);
                hashMap.put(nums[i],a+1);
                if((a+1)>(length/2)) {
                    num = nums[i];
                    break;
                }
            } else {
                hashMap.put(nums[i],1);
            }
        }
        if(num == 0) {
            num = nums[length-1];
        }
        return num;
    }
}